package com.pinyoumei.app.activity.ShoppingCenter.pdd;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.pinyoumei.app.R;
import com.pinyoumei.app.activity.BaseActivity;
import com.pinyoumei.app.activity.ShoppingCenter.ShopItemDetailActivity;
import com.pinyoumei.app.adapter.Pdd99Adapter;
import com.pinyoumei.app.bean.JD99Bean;
import com.pinyoumei.app.bean.ShopCenterMainBean;
import com.pinyoumei.app.network.Constants;
import com.pinyoumei.app.utils.SPUtils;
import com.pinyoumei.app.utils.SignatureUtils;
import com.pinyoumei.app.utils.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2020-12-17.
 * --------- 日期 ------- 维护人 ------------ 变更内容 --------
 * 2020-12-17        Administrator             新增Person类
 * 2020-12-17        Administrator             增加sex属性
 * 拼多多专区
 */
public class PddListActivity extends BaseActivity implements View.OnClickListener {

//    private Banner banner;
    private SmartRefreshLayout swipeRefreshLayout;
    private List<String> image_list = new ArrayList<>();
    private Pdd99Adapter Pdd99Adapter;
    private String channel_type;
    private String title_name;
    private int page = 1;
    private List<JD99Bean.ResultBean.GoodsListBean> goods_list = new ArrayList<>();
    private String list_id = "";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_99_jd);

        channel_type = getIntent().getStringExtra("channel_id");
        title_name = getIntent().getStringExtra("title_name");


        initWidget();



        if (channel_type!=null){
            initData();
        }

    }

    private void initWidget() {
        TextView tv_title = findViewById(R.id.tv_title);
        if (title_name!=null){
            tv_title.setText(title_name);
        }



        RelativeLayout relativeFinish = findViewById(R.id.relative_finish);
        relativeFinish.setOnClickListener(this);
//        banner = findViewById(R.id.banner2);
        swipeRefreshLayout = findViewById(R.id.swipeRefreshLayout);
        swipeRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                goods_list.clear();
                page = 1;
                initData();
                swipeRefreshLayout.finishRefresh();
            }
        });
        swipeRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                loadmoreData();
                swipeRefreshLayout.finishLoadMore();
            }
        });
        RecyclerView rv1 = findViewById(R.id.rv_1);
        rv1.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
        Pdd99Adapter = new Pdd99Adapter(R.layout.item_pdd99_list,null);
        rv1.setAdapter(Pdd99Adapter);
    }

    private void initData() {

        Map<String, Object> map = new HashMap<>();
        map.put("user_id",  SPUtils.getAppUserId(PddListActivity.this));
        map.put("channel_type", channel_type);
        map.put("list_id", list_id);
        map.put("page", page);
        map.put("limit", 10);


        OkGo.<String>post(Constants.PDD_GOOD_LIST)
                .params("user_id",  SPUtils.getAppUserId(PddListActivity.this))
                .params("channel_type", channel_type)
                .params("list_id", list_id)
                .params("page", page)
                .params("limit", 10)
                .params("sign", SignatureUtils.createSignature(map, Constants.SIGN_SECRET))
                .execute(new StringCallback() {


                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                        loadingDailog.show();
                    }


                    @Override
                    public void onSuccess(Response<String> response) {
                        setData(response);

                    }

                    private void setData(Response<String> response) {
                        loadingDailog.dismiss();
                        JD99Bean jd99Bean = gson.fromJson(response.body(), JD99Bean.class);
                        if (jd99Bean.getCode() >= 0) {
//                            if(jd99Bean.getResult().getTop_banner_list()!=null && jd99Bean.getResult().getTop_banner_list().size()>0){
//                                initBanner(jd99Bean.getResult().getTop_banner_list());
//                            }
                            goods_list.addAll(jd99Bean.getResult().getGoods_list());
                            Pdd99Adapter.setNewData(goods_list);
                            list_id = jd99Bean.getResult().getList_id();

                            Pdd99Adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                                @Override
                                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                                    Intent intent = new Intent(PddListActivity.this, ShopItemDetailActivity.class);
                                    Bundle bundle = new Bundle();
                                    ShopCenterMainBean.ResultBean.ShowGoodsListBean.GoodsListBean goodsListBean = goods_list.get(position);
                                    bundle.putString("type","pdd");
                                    bundle.putSerializable("goodsListBean", goodsListBean);
                                    intent.putExtras(bundle);
                                    startActivity(intent);
                                }
                            });
                        } else {
                            ToastUtils.showShort(jd99Bean.getMsg());
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDailog.dismiss();
                    }
                });


    }




    private void loadmoreData() {

        Map<String, Object> map = new HashMap<>();
        map.put("user_id",  SPUtils.getAppUserId(PddListActivity.this));
        map.put("channel_type", channel_type);
        map.put("list_id", list_id);
        map.put("page", page);
        map.put("limit", 10);


        OkGo.<String>post(Constants.JD_GOOD_LIST)
                .params("user_id",  SPUtils.getAppUserId(PddListActivity.this))
                .params("channel_type", channel_type)
                .params("list_id", list_id)
                .params("page", page)
                .params("limit", 10)
                .params("sign", SignatureUtils.createSignature(map, Constants.SIGN_SECRET))
                .execute(new StringCallback() {


                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                        loadingDailog.show();
                    }


                    @Override
                    public void onSuccess(Response<String> response) {
                        setData(response);

                    }

                    private void setData(Response<String> response) {
                        loadingDailog.dismiss();
                        JD99Bean jd99Bean = gson.fromJson(response.body(), JD99Bean.class);
                        if (jd99Bean.getCode() >= 0) {

                            Pdd99Adapter.addData(jd99Bean.getResult().getGoods_list());

                        } else {
                            ToastUtils.showShort(jd99Bean.getMsg());
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDailog.dismiss();
                    }
                });


    }

//    private void initBanner(List<JD99Bean.ResultBean.TopBannerListBean> banner_list) {
//        //第一个Banner
//        for (int i = 0; i < banner_list.size(); i++) {
//            image_list.add(banner_list.get(i).getImg_path());
//        }
//        if (image_list.size() == 1) {
//            banner.isAutoPlay(false);//设置默认为不轮播
//
//        } else {
//            banner.isAutoPlay(true);//设置默认为自动轮播
//
//        }
//        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);//设置Banner的样式BannerConfig.NUM_INDICATOR
//        banner.setImages(image_list); //存放图片的list集合url
//        banner.setBannerAnimation(Transformer.ZoomOutSlide);//设置Banner轮播的动画特效的样式
//        //banner.setPageTransformer(true,)//自定义轮播动画
//        banner.setImageLoader(new GlideImageLoader());//设置图片加载器
//        banner.setDelayTime(3000);//设置轮播间隔时间
//        //设置指示器位置（当banner模式中有指示器时）
//        banner.setIndicatorGravity(BannerConfig.CENTER);
//        banner.start();
//
//        banner.setOnBannerListener(new OnBannerListener() {
//            @Override
//            public void OnBannerClick(int position) {
//                if (banner_list.get(position).getIs_can_link() == 1) {
//
//                    if (banner_list.get(position).getIs_h5() == 1) {
//                        Intent intent = new Intent(PddListActivity.this, WebViewActivity.class);
//                        intent.putExtra("url", banner_list.get(position).getH5_url());
//                        intent.putExtra("order_title", "");
//                        startActivity(intent);
//                    } else {
//                        //跳转
//                        IntentUtils.Intent_to(PddListActivity.this, banner_list.get(position).getMethod_name(),"");
//                    }
//
//                }
//
//
//            }
//        });
//
//    }

//    //使用Glide加载器
//    public class GlideImageLoader extends ImageLoader {
//        @Override
//        public void displayImage(Context context, Object path, ImageView imageView) {
//            //Glide 加载图片
//            RequestOptions placeholder = new RequestOptions().placeholder(R.mipmap.ic_error_loading);
//            Glide.with(context).load(Constants.MAIN_URL + path).apply(placeholder).into(imageView);
//        }
//    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        	case R.id.relative_finish:
        		finish();
        		break;

        	default:
        		break;
        }
    }
}
